/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Jocreatulary_GUI.java
 *
 * Created on 16.12.2010, 19:36:54
 */
package de.pitkley.jocreatulary;

import java.awt.event.KeyEvent;

/**
 *
 * @author pit
 */
public class Jocreatulary_GUI extends javax.swing.JFrame {

    /** Creates new form Jocreatulary_GUI */
    public Jocreatulary_GUI() {
        initComponents();

        vocabList.add("Neue Vokabel..");
        vocabList.select(0);
        tfVocableToTranslate.requestFocus();

        ctrl = new Jocreatulary_Controller(this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pVocab = new javax.swing.JPanel();
        lblNewVocabfile = new javax.swing.JLabel();
        tfVocabfileName = new javax.swing.JTextField();
        btnCreateList = new javax.swing.JButton();
        lblInfo = new javax.swing.JLabel();
        sep1 = new javax.swing.JSeparator();
        jPanel1 = new javax.swing.JPanel();
        vocabList = new java.awt.List();
        jPanel2 = new javax.swing.JPanel();
        lblVocableToTranslate = new javax.swing.JLabel();
        tfVocableToTranslate = new javax.swing.JTextField();
        lblTranslation = new javax.swing.JLabel();
        tfTranslation = new javax.swing.JTextField();
        lblAlternativeTranslation = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lblNewVocabfile.setLabelFor(tfVocabfileName);
        lblNewVocabfile.setText("Name für Vokabelliste eingeben:");

        btnCreateList.setText("Vokabelliste erstellen");
        btnCreateList.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreateListActionPerformed(evt);
            }
        });

        lblInfo.setText("<html>\nDamit deine erstellte Vokabelliste in Jocabulary angezeigt wird, musst du folgende Schritte tun:<br/>\n<ul>\n<li>Einen Namen für die Datei eingeben</li>\n<li>Den Button \"Vokabelliste erstellen\" drücken</li>\n<li>Die Datei \"Dein-Dateiname.xml\" im Verzeichnis von Jocreatulary an pitkley@gmail.com schicken</li>\n</ul>\n</html>");

        org.jdesktop.layout.GroupLayout pVocabLayout = new org.jdesktop.layout.GroupLayout(pVocab);
        pVocab.setLayout(pVocabLayout);
        pVocabLayout.setHorizontalGroup(
            pVocabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(pVocabLayout.createSequentialGroup()
                .addContainerGap()
                .add(pVocabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(pVocabLayout.createSequentialGroup()
                        .add(sep1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 690, Short.MAX_VALUE)
                        .addContainerGap())
                    .add(pVocabLayout.createSequentialGroup()
                        .add(lblInfo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(52, Short.MAX_VALUE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, pVocabLayout.createSequentialGroup()
                        .add(pVocabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, tfVocabfileName, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)
                            .add(btnCreateList, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE))
                        .add(502, 502, 502))
                    .add(pVocabLayout.createSequentialGroup()
                        .add(lblNewVocabfile)
                        .addContainerGap(506, Short.MAX_VALUE))))
        );
        pVocabLayout.setVerticalGroup(
            pVocabLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, pVocabLayout.createSequentialGroup()
                .addContainerGap()
                .add(lblInfo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 15, Short.MAX_VALUE)
                .add(sep1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(lblNewVocabfile)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(tfVocabfileName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(btnCreateList)
                .addContainerGap())
        );

        vocabList.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                vocabListItemStateChanged(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(vocabList, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 265, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(vocabList, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 228, Short.MAX_VALUE)
                .addContainerGap())
        );

        lblVocableToTranslate.setText("Zu übersetzendes Wort (Deutsch):");

        tfVocableToTranslate.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfVocableToTranslateKeyPressed(evt);
            }
        });

        lblTranslation.setText("Übersetzung (Englisch):");

        tfTranslation.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tfTranslationKeyPressed(evt);
            }
        });

        lblAlternativeTranslation.setText("Alternative Übersetzung:");

        jTextField1.setEditable(false);
        jTextField1.setText("Fertig!");
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextField1KeyPressed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(lblVocableToTranslate, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE)
                    .add(lblTranslation, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE)
                    .add(lblAlternativeTranslation, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE)
                    .add(tfVocableToTranslate, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE)
                    .add(tfTranslation, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 274, Short.MAX_VALUE)
                    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(lblVocableToTranslate)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(tfVocableToTranslate, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(lblTranslation)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(tfTranslation, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(lblAlternativeTranslation)
                .add(44, 44, 44)
                .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(pVocab, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, pVocab, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tfVocableToTranslateKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfVocableToTranslateKeyPressed
        if (evt.getKeyChar() == KeyEvent.VK_ENTER) {
            if (tfVocableToTranslate.getText().equals("")) {
                tfTranslation.requestFocus();
            } else {
                ctrl.addNativeWord(tfVocableToTranslate.getText());
                tfVocableToTranslate.setText("");
            }
        }
    }//GEN-LAST:event_tfVocableToTranslateKeyPressed

    private void tfTranslationKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfTranslationKeyPressed
        if (evt.getKeyChar() == KeyEvent.VK_ENTER) {
            if (tfTranslation.getText().equals("")) {
                addVocable();
            } else {
                ctrl.addTranslation(tfTranslation.getText());
                tfTranslation.setText("");
            }
        }
    }//GEN-LAST:event_tfTranslationKeyPressed

    private void vocabListItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_vocabListItemStateChanged
        int selectedIndex = vocabList.getSelectedIndex();
        // CTRL -> GetVocable(index = selectedIndex)
        // Vocable -> GetVocableToTranslate :: GetTranslation :: GetAlternativeTranslation
        // tfVocableToTranslate <- VocableToTranslate
        // tfTranslation <- Translation
        // tfAlternativeTranslation <- AlternativeTranslation
        /*Vocable v = ctrl.getVocable(selectedIndex);
        tfVocableToTranslate.setText(v.getNativeWord());
        tfTranslation.setText(v.getTranslation());
        tfAlternativeTranslation.setText(v.getAlternativeTranslation());*/
    }//GEN-LAST:event_vocabListItemStateChanged

    private void btnCreateListActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCreateListActionPerformed
        // CTRL -> CreateList(listName : String = tfVocabfileName.getText())
        //ctrl.writeFile(tfVocabfileName.getText());
        ctrl.writeJSON(tfVocabfileName.getText());
    }//GEN-LAST:event_btnCreateListActionPerformed

    private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyPressed
        if (evt.getKeyChar() == KeyEvent.VK_ENTER) {
            addVocable();
        }
    }//GEN-LAST:event_jTextField1KeyPressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Jocreatulary_GUI().setVisible(true);
            }
        });
    }

    public void addVocable() {
        /*int selectedIndex = vocabList.getSelectedIndex();
        int lastIndex = vocabList.getItemCount() - 1;
        
        if (selectedIndex == lastIndex) {
        String toTranslate = tfVocableToTranslate.getText();
        String translation = tfTranslation.getText();
        String alternativeTranslation = tfAlternativeTranslation.getText();
        
        vocabList.add(toTranslate + " - " + translation + ((!alternativeTranslation.equals("")) ? (" / " + alternativeTranslation) : ("")), lastIndex);
        // CTRL -> AddVocable(toTranslate: String, translation: String, alternativeTranslation: String)
        ctrl.addVocable(toTranslate, translation, alternativeTranslation);
        } else {
        String toTranslate = tfVocableToTranslate.getText();
        String translation = tfTranslation.getText();
        String alternativeTranslation = tfAlternativeTranslation.getText();
        
        vocabList.replaceItem(toTranslate + " - " + translation + ((!alternativeTranslation.equals("")) ? (" / " + alternativeTranslation) : ("")), selectedIndex);
        // CTRL -> UpdateVocable(index: int, toTranslate: String, translation: String, alternativeTranslation: String)
        ctrl.updateVocable(selectedIndex, toTranslate, translation, alternativeTranslation);
        }*/
        ctrl.addVocable();
        tfVocableToTranslate.setText("");
        tfTranslation.setText("");
        //tfAlternativeTranslation.setText("");
        tfVocableToTranslate.requestFocus();
        //vocabList.select(lastIndex + 1);

    }
    private Jocreatulary_Controller ctrl;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCreateList;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JLabel lblAlternativeTranslation;
    private javax.swing.JLabel lblInfo;
    private javax.swing.JLabel lblNewVocabfile;
    private javax.swing.JLabel lblTranslation;
    private javax.swing.JLabel lblVocableToTranslate;
    private javax.swing.JPanel pVocab;
    private javax.swing.JSeparator sep1;
    private javax.swing.JTextField tfTranslation;
    private javax.swing.JTextField tfVocabfileName;
    private javax.swing.JTextField tfVocableToTranslate;
    private java.awt.List vocabList;
    // End of variables declaration//GEN-END:variables
}
